package com.xxx.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.xxx.common.Code;
import com.xxx.common.Result;
import com.xxx.dto.DishDto;
import com.xxx.pojo.ClassInfo;
import com.xxx.pojo.Dish;
import com.xxx.service.ClassInfoService;
import com.xxx.service.DishService;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresGuest;
import org.apache.shiro.authz.annotation.RequiresRoles;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.stream.Collectors;

@Slf4j
@RestController
@RequestMapping("/dish")
public class DishController {

    @Autowired
    private ClassInfoService classInfoService;

    @Autowired
    private DishService dishService;

    @RequiresRoles(value = {"admin","manage","employee"},logical = Logical.OR)
    @PutMapping("/update")
    public Result updateDish(@RequestBody Dish dish){

        boolean update = dishService.updateById(dish);

        Result result = new Result();
        if (update){
            result.setCode(Code.UPDATE_OK);
            result.setMessage("修改成功！！！");
        }else {
            result.setCode(Code.UPDATE_ERR);
            result.setMessage("系统出错 修改失败！！！");
        }
        return result;
    }

    /**
     * 添加菜品
     * @param dish
     * @return
     */

    @RequiresRoles(value = {"admin","manage","employee"},logical = Logical.OR)
    @PostMapping
    public Result save(@RequestBody Dish dish){
        log.info("添加的菜品：{}",dish);
        boolean save = dishService.save(dish);
        Result result = new Result();
        if (save){
            result.setCode(Code.SAVE_OK);
            result.setMessage("添加成功！！");
        }else {
            result.setCode(Code.SAVE_ERR);
            result.setMessage("500 系统出错，添加失败！！");
        }
        return result;
    }

    /**
     * 起售和禁售
     * @param status
     * @param ids
     * @return
     */
    @RequiresRoles(value = {"admin","manage","employee"},logical = Logical.OR)
    @PutMapping("/{status}/{ids}")
    public Result updateStatus(@PathVariable Integer status,@PathVariable List<Long> ids){
        log.info("ids:{},更新的状态:{}",ids,status);
        int i = dishService.updateStatus(ids, status);
        Result result = new Result();

        if (i > 0){
            result.setCode(Code.UPDATE_OK);
            result.setMessage("修改成功！！！");
        }else {
            result.setCode(Code.UPDATE_ERR);
            result.setMessage("未知错误！！！");
        }
        return result;
    }

    /**
     * 根据id删除
     * @param ids
     * @return
     */
    @RequiresRoles(value = {"admin","manage"},logical = Logical.OR)
    @DeleteMapping("/{ids}")
    public Result deleteByIds(@PathVariable List<Long> ids){
        log.info("删除的ids:{}",ids);
        boolean b = dishService.removeBatchByIds(ids);
        Result r = new Result();
        if (b){
            r.setCode(Code.DELETE_OK);
            r.setMessage("删除成功");
        }else{
            r.setCode(Code.DELETE_ERR);
            r.setMessage("未知错误！！！");
        }
        return r;
    }

    /**
     * 分页查询
     * @param pageNo
     * @param pageSize
     * @param name
     * @return
     */
    @RequiresRoles(value = {"admin","manage","employee"},logical = Logical.OR)
    @GetMapping
    public Result getByPage(int pageNo,int pageSize,String name){
        IPage iPage = new Page(pageNo,pageSize);

        IPage page = dishService.getByPage(iPage, name);

        List<Dish> records = page.getRecords();


        List<DishDto> dishDtoList = records.stream().map(dish -> {
            DishDto dishDto = new DishDto();

            BeanUtils.copyProperties(dish,dishDto, DishDto.class);
            ClassInfo classInfo = classInfoService.getTypesNameById(dish.getTypesId());
            dishDto.setTypesName(classInfo.getTypesName());
            log.info("分类名称：{}",classInfo.getTypesName());
            return dishDto;
        }).collect(Collectors.toList());
        log.info("菜品详情：{}",dishDtoList);
        page.setRecords(dishDtoList);
        Result result = new Result(page, Code.GET_OK);
        return result;
    }



    @RequiresGuest
    @GetMapping("/list")
    public Result getDish(Long id){
        List<Dish> dishList = dishService.list(new LambdaQueryWrapper<Dish>().eq(Dish::getTypesId, id).eq(Dish::getStatus,1));

        return dishList != null ? new Result(dishList,Code.GET_OK) : new Result(null,Code.GET_ERR,"无数据");
    }

}
